package com.chuangjiangx.member.manager.client.web.basic.controller;

import com.chuangjiangx.commons.response.Response;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.member.basic.ddd.dal.dto.*;
import com.chuangjiangx.member.basic.ddd.query.MbrAnalysisQuery;
import com.chuangjiangx.member.manager.basic.web.interceptor.Login;
import com.chuangjiangx.member.manager.client.web.basic.request.MbrAanalysisRequest;
import com.chuangjiangx.member.manager.client.web.basic.response.AnalysisByChannelResponse;
import com.chuangjiangx.member.manager.client.web.basic.response.AnalysisResponse;
import com.chuangjiangx.member.manager.common.Constants;
import com.chuangjiangx.member.manager.common.ThreadLocalUser;
import com.chuangjiangx.member.stored.ddd.dal.condition.AppOrderCountCondition;
import com.sun.jmx.snmp.ThreadContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 会员分析接口
 * @author cj-yzh
 * @date 2018/8/7 下午2:05
 */
@Api(value = "会员分析",tags = "统计")
@RequestMapping("/mbr/analysis")
@RestController
public class MbrAnalysisController {

    @Autowired
    private MbrAnalysisQuery mbrAnalysisQuery;


    @Login
    @RequestMapping("/consumer-analysis")
    @ApiOperation(value = "会员消费分析查询接口",httpMethod = "GET")
    public Response mbrAnalysis(MbrAanalysisRequest request){

        //获得登陆对象
        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        long merchantId = user.getMerchantId();

        //返回结果集
        AnalysisResponse response = new AnalysisResponse();
        //设置商户id
        response.setMerchantId(merchantId);
        //查询条件
        AppOrderCountCondition condition = new AppOrderCountCondition();
        condition.setStart(request.getStartTime());
        condition.setEnd(request.getEndTime());
        condition.setMerchantId(merchantId);
        //查询总人数和新增人数
        MbrCount mbrCount = mbrAnalysisQuery.queryCount(condition);
        response.setTotalMembers(mbrCount.getTotalMembers());
        response.setNewMembers(mbrCount.getNewMembers());
        response.setCountType("TIME");
        //默认按时间查询
        List<TimeScales> timeScales = mbrAnalysisQuery.queryByTime(condition);
       response.setTimeScales(timeScales);

        //会员活跃分析
        List<Actives> mbrActiveScales = mbrAnalysisQuery.queryByScope(condition);
        response.setActives(mbrActiveScales);
        return ResponseUtils.successCamel(response);
    }



    @ApiOperation(value = "根据渠道分析会员",httpMethod = "GET")
    @RequestMapping(value = "/consumer-analysis-sub",method = RequestMethod.GET)
    @Login
    public Response analysisByChannel(MbrAanalysisRequest request){
        //获得登陆对象
        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        long merchantId = user.getMerchantId();
        //查询渠道
        String channel = request.getCountType();
        //返回结果集
        AnalysisByChannelResponse channelResponse = new AnalysisByChannelResponse();
        //设置商户id
        channelResponse.setMerchantId(merchantId);
        //设置查询渠道
        channelResponse.setCountType(channel);
        //查询条件
        AppOrderCountCondition condition = new AppOrderCountCondition();
        condition.setStart(request.getStartTime());
        condition.setEnd(request.getEndTime());
        condition.setMerchantId(merchantId);
        //查询总数
        MbrCount mbrCount = mbrAnalysisQuery.queryCount(condition);
        condition.setMerchantId(merchantId);
        //按终端查询
        if("TERMINAL".equals(channel)){
            List<TerminalScales> terminalScales = mbrAnalysisQuery.queryByTiminal(condition);
            for (TerminalScales scale : terminalScales){
                scale.setTotalMembers(mbrCount.getTotalMembers());
            }
            channelResponse.setTerminalScales(terminalScales);
        }
        //按性别查询
        else if("SEX".equals(channel)){
            List<SexScales> sexScales = mbrAnalysisQuery.queryBySex(condition);
            channelResponse.setSexScales(sexScales);
        }
        //按门店
        else if("STORE".equals(channel)){
            List<StoreScales> storeScales = mbrAnalysisQuery.queryByStore(condition);
            for (StoreScales scale : storeScales){
                scale.setTotalMembers(mbrCount.getTotalMembers());
            }
            channelResponse.setStoreScales(storeScales);
        }
        //按时间
        else{
            channelResponse.setCountType("TIME");
            List<TimeScales> timeScales = mbrAnalysisQuery.queryByTime(condition);
            channelResponse.setTimeScales(timeScales);
        }
        return ResponseUtils.successCamel(channelResponse);
    }
}
